$(document).ready(function(){
      $("#changingPassword").hide();
       var ajaxLoading=false;
       $("#toChangePassword").click(function(){
                    $("#changingOthers").hide();
                    $("h3").text("Changing password");
                    $("#changingPassword").show();
       });

       $("#toChangeOthers").click(function(){
                $("#changingPassword").hide() ;
                $("h3").text("Editing Profile");
                $("#changingOthers").show();
       });


       $('.toChangePhone').blur(function() {
              if($(this).val()!=$("input[name='toChangePhoneHidden']").val()){
                   $("input[name='isSomeChanged']").val(1);
              }
              $("input[name='isNormalOthers']").val(1);
       });

       $('.toChangeName').blur(function() {
              if($(this).val()!=$("input[name='toChangeNameHidden']").val()){
                   $("input[name='isSomeChanged']").val(1);
              }
              $("input[name='isNormalOthers']").val(1);
       });

       $('.toChangeSurname').blur(function() {

              if($(this).val()!=$("input[name='toChangeSurnameHidden']").val()){
                   $("input[name='isSomeChanged']").val(1);
              }
              $("input[name='isNormalOthers']").val(1);
          });


       $('.toChangeUsername').blur(function() {

                      if ($(this).val().length>=5) {
                          var username= $(this).val();
                          var toChangeUsernameHidden=$("input[name='toChangeUsernameHidden']").val();
                          if(username!=toChangeUsernameHidden){
                              $("input[name='isSomeChanged']").val(1);
                              $.ajax({
                                  url:'checkUsername.action',
                                  data:"userName="+username,
                                  type:'POST',
                                  cache:false,
                                  dataType:'json' ,
                                       success:function(data){
                                           if(data.result!=""){
                                              $("#validtoChangeUsername").text(data.result);
                                               $("input[name='isNormalOthers']").val(0);
                                           }else{
                                              var ok="<img src='images/ok/ok.png' width='16' height='16' />"  ;
                                              $("#validtoChangeUsername").html(ok);
                                               $("input[name='isNormalOthers']").val(1);
                                           }
                                       }
                              });
                         } else{
                                 $("#validtoChangeUsername").html("");
                                 $("input[name='isNormalOthers']").val(1);
                          }
                       }  else if($(this).val().length==0){
                              $("#validtoChangeUsername").html("");
                              $("#validtoChangeUsername").text(" can't be empty!");
                              $("input[name='isNormalOthers']").val(0);

                         }else{
                              $("#validtoChangeUsername").html("");
                              $("#validtoChangeUsername").text(" less than 5 letters!");
                              $("input[name='isNormalOthers']").val(0);

                      }

          });

        $(".toChangeManagerEmail").blur(function(){
                           if ($(this).val().length>0) {
                          var email= $(this).val();

                          var toChangeManagerEmailHidden=$("input[name='toChangeManagerEmailHidden']").val();

                         if(email!=toChangeManagerEmailHidden){

                             $("input[name='isSomeChanged']").val(1);

                             $.ajax({
                                  url:'checkEmail.action',
                                  data:"email="+email,
                                  type:'POST',
                                  cache:false,
                                  dataType:'json' ,
                                       success:function(data){

                                           if(data.result!=""){
                                              $("#validtoChangeManagerEmail").text(data.result);
                                               $("input[name='isNormalOthers']").val(0);

                                           }else{
                                              var ok="<img src='images/ok/ok.png' width='16' height='16' />"  ;
                                              $("#validtoChangeManagerEmail").html(ok);
                                               $("input[name='isNormalOthers']").val(1);

                                           }
                                         }
                              });
                         } else{
                                  $("#validtoChangeManagerEmail").html("");
                                  $("input[name='isNormalOthers']").val(1);
                          }
                       }  else{
                          $("#validtoChangeManagerEmail").html("");
                          $("#validtoChangeManagerEmail").text(" can't be empty!");
                           $("input[name='isNormalOthers']").val(0);
                      }

        })


       $('.toChangeEmail').blur(function() {

                      if ($(this).val().length>0) {
                          var email= $(this).val();

                          var toChangeEmailHidden=$("input[name='toChangeEmailHidden']").val();

                         if(email!=toChangeEmailHidden){

                              $("input[name='isSomeChanged']").val(1);

                             $.ajax({
                                  url:'checkEmail.action',
                                  data:"email="+email,
                                  type:'POST',
                                  cache:false,
                                  dataType:'json' ,
                                       success:function(data){

                                           if(data.result!=""){
                                              $("#validtoChangeEmail").text(data.result);
                                               $("input[name='isNormalOthers']").val(0);
                                             }else{
                                              var ok="<img src='images/ok/ok.png' width='16' height='16' />"  ;
                                              $("#validtoChangeEmail").html(ok);
                                               $("input[name='isNormalOthers']").val(1);
                                            }
                                         }
                              });
                          } else{
                                  $("#validtoChangeEmail").html("");
                                  $("input[name='isNormalOthers']").val(1);
                          }
                       }  else{
                          $("#validtoChangeEmail").html("");
                          $("#validtoChangeEmail").text(" can't be empty!");
                           $("input[name='isNormalOthers']").val(0);
                      }

          });

       $("#saveNewProfile").click(function(){
         if (!ajaxLoading) {
            ajaxLoading = true;
            var isNormal=$("input[name='isNormalOthers']").val();
            if(isNormal==1){
                   var isSomeChanged=$("input[name='isSomeChanged']").val();
                  if(isSomeChanged==0){
//                       alert("There is nothing to change!");
                      ajaxLoading = false;
                       window.location.href = 'profileViewAdmin.action';
                  }else{
                  var newUserName=$(".toChangeUsername").val();

                 var newEmail=$(".toChangeEmail").val();

                 var newName=$(".toChangeName").val();

                 var newSurname=$(".toChangeSurname").val();

                var newPhone=$(".toChangePhone").val();

                var newManagerEmail=$(".toChangeManagerEmail").val();
                    if(newManagerEmail!="undefined"){
                        var toChangeManagerEmailHidden=$("input[name='toChangeManagerEmailHidden']").val();

                    }else{
                        newManagerEmail="";
                    }

                 $.ajax({
                                      url:'changeProfile.action',
                                      data:{
                                          newUserName: newUserName,
                                          newEmail : newEmail,
                                          newName : newName,
                                          newSurname : newSurname,
                                          newPhone : newPhone,
                                          newManagerEmail :newManagerEmail
                                      },
                                      type:'POST',
                                      cache:false,
                                      dataType:'json' ,
                                           success:function(data){
                                              ajaxLoading = false;
                                              alert(data.result);
                                              window.location.href = data.toRedirect;
                                           }
                  });
            }
            }
            else{
                alert("Sorry , but your profile can't be changed! There are some incorrect data.");
            }
             ajaxLoading = false;
         }
       });
       $("#changeOldPassword").blur(function(){
                var oldPassword=$(this).val();
                        if(oldPassword.length>0){
                                   $.ajax({
                                          url:'checkOldPassword.action',
                                          data:"oldPassword="+ oldPassword,
                                          type:'POST',
                                          cache:false,
                                          dataType:'json' ,
                                               success:function(data){

                                                   if(data.result!=""){
                                                       $("#validOldPassword1").html("Is incorrect");
                                                       $("input[name='isNormalPassword']").val(0);
                                                   }else{
                                                         var ok="<img src='images/ok/ok.png' width='16' height='16' />";
                                                         $("#validOldPassword1").html(ok);
                                                         $("input[name='isNormalPassword']").val(1);
                                                   }
                                                 }
                                      });

                                   var newPass=$("#createNewPassword").val();
                                   if(newPass.length>0){
                                       if(oldPassword==newPass){
                                           $("#validNewPassword").text("The passwords are the same!");
                                           $("input[name='isNormalPassword']").val(0);
                                       }
                                   }



                        }  else{
                             $("input[name='isNormalPassword']").val(0);
                        }
        });

      $("#createNewPassword").blur(function(){
            var oldPass=$("#changeOldPassword").val();
            var newPass=$("#createNewPassword").val();
            if(oldPass.length>0){
                  if(newPass.length>0){
                      if(newPass.length>=5){
                              if(oldPass==newPass){
                                          $("#validNewPassword").text("The passwords are the same!");
                                          $("input[name='isNormalPassword']").val(0);
                               }else{
                                  var ok="<img src='images/ok/ok.png' width='16' height='16' />"  ;
                                  $("#validNewPassword").html(ok);
                                  $("input[name='isNormalPassword']").val(1);
                               }
                      }else{
                           $("#validNewPassword").text("Less than 5 letter!");
                           $("input[name='isNormalPassword']").val(0);
                      }
                    } else{
                       $("#validNewPassword").text("Insert the new password!");
                        $("input[name='isNormalPassword']").val(0);
                    }
            }else{
                  if(newPass.length>0 && newPass.length<5){
                      $("#validOldPassword1").text("Insert old password!");
                      $("#validNewPassword").text("Less than 5 letter!");
                      $("input[name='isNormalPassword']").val(0);
                   }

            }
       });

      $("#confirmNewPassword").blur(function(){
            var newPass=$("#createNewPassword").val();
            var cnfPass=$("#confirmNewPassword").val();
          if(cnfPass.length>0){
              if(newPass==cnfPass){
                           var ok="<img src='images/ok/ok.png' width='16' height='16' />"  ;
                           $("#validCnfPassword1").html(ok);
                           $("input[name='isNormalPassword']").val(1);

               }else{
                          $("#validCnfPassword1").text("Confirm new password!");
                           $("input[name='isNormalPassword']").val(0);
               }
          }  else{
               $("input[name='isNormalPassword']").val(0);
               $("#validCnfPassword1").text("");
          }
       });

       $("#saveNewPassword").click(function(){
           if (!ajaxLoading) {
                ajaxLoading = true;
                var isNormal= $("input[name='isNormalPassword']").val();
                if(isNormal==1){
                    var newPassword= $("#createNewPassword").val();
                         $.ajax({
                                          url:'changePassword.action',
                                          data:"newPassword="+ newPassword,
                                          type:'POST',
                                          cache:false,
                                          dataType:'json' ,
                                               success:function(data){

                                                      alert(data.result);
                                                    if(data.isOk==1){
                                                             $("#changeOldPassword").val("");
                                                                  $("#validOldPassword1").html("");
                                                                  $("#validOldPassword1").text("");
                                                             $("#createNewPassword").val("");
                                                                  $("#validNewPassword").text("");
                                                                  $("#validNewPassword").html("");
                                                             $("#confirmNewPassword").val("");
                                                                  $("#validCnfPassword1").text("");
                                                                  $("#validCnfPassword1").html("");
                                                     }
                                                  }
                                      });
                }      else {
                    alert("Sorry , but your password can't be changed! There are some incorrect data.");
                }
                    ajaxLoading=false;
           }
        })

        $(".closeWindow").click(function (e) {
                  $("#changeProfile").hide("slow");
                  $('#grayBox').css("width", "0px");
                  $('#grayBox').css("height", "0px");
          });

         $("#cancelProfile").click(function(){
            $("#validtoChangeEmail").html("");
            $("#validtoChangeUsername").html("");
            $(".toChangeUsername").val($("input[name='toChangeUsernameHidden']").val());
            $(".toChangeEmail").val($("input[name='toChangeEmailHidden']").val());

        });

         $("#cancelPassword").click(function(){
            $("#validOldPassword1").html("");
            $("#validNewPassword").html("");
            $("#validCnfPassword1").html("");
            $("#createNewPassword").val("");
            $("#changeOldPassword").val("");
            $("#confirmNewPassword").val("");
         })  ;
});